﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 盎宇宙服务
{
    public class SystemTray
    {
        public static SystemTray Instance;
        /// <summary>
        /// 静态构造函数,在类第一次被创建或者静态成员被调用的时候调用
        /// </summary>
        static SystemTray()
        {
            Instance = new SystemTray();
        }

        public NotifyIcon Ni { get; set; }

        private SystemTray()
        {
            Ni = new System.Windows.Forms.NotifyIcon();
            Stream iconStream = App.GetResourceStream(new Uri("pack://application:,,,/盎宇宙服务;component/logo.ico")).Stream;
            Ni.Icon = new System.Drawing.Icon(iconStream);
            Ni.Visible = true;
            Ni.Text = "盎宇宙关机服务";
            Ni.MouseClick += Ni_MouseClick;
        }
        AutoStartServer autoStartServer = new AutoStartServer();
        private void Ni_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Right)
                {
                    Ni.ContextMenu = null;
                    List<System.Windows.Forms.MenuItem> menuItems = new List<System.Windows.Forms.MenuItem>();

                    //if (autoStartServer.isAutoStart())
                    //{
                    //    System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("取消开机启动");
                    //    exit.Click += CancelAutoStart;
                    //    menuItems.Add(exit);
                    //}
                    //else
                    //{
                    //    System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("开机启动");
                    //    exit.Click += AutoStart;
                    //    menuItems.Add(exit);
                    //}

                    System.Windows.Forms.MenuItem exit1 = new System.Windows.Forms.MenuItem("退出");
                    exit1.Click += Exit_Click;
                    menuItems.Add(exit1);


                    Ni.ContextMenu =
                      new System.Windows.Forms.ContextMenu(menuItems.ToArray());

                    //System.Windows.Controls.ContextMenu NotifyIconMenu = (System.Windows.Controls.ContextMenu)App.Current.FindResource("NotifyIconMenu");
                    //NotifyIconMenu.IsOpen = true;
                    //App.Current.MainWindow?.Activate();
                }

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           

            //if (e.Button == MouseButtons.Left)
            //{
            //    var wins = WindowsManager.Instance.Windows;
            //    foreach (var i in wins)
            //    {
            //        i.Activate();
            //    }
            //}
        }

        private void AutoStart(object sender, EventArgs e)
        {
         
            try
            {
                autoStartServer.SetMeAutoStart(true);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void CancelAutoStart(object sender, EventArgs e)
        {
            try
            {
                autoStartServer.SetMeAutoStart(false);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            DisposeNotifyIcon();
            App.Current.Shutdown();
            Process.GetCurrentProcess().Kill();
            //   throw new NotImplementedException();
        }

        /// <summary>
        /// 销毁系统托盘图标的资源
        /// </summary>
        public void DisposeNotifyIcon()
        {
            Ni?.Dispose();
        }

    }
}
